from django.shortcuts import render
from django.http.response import *
from django.contrib import auth
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticated
from rest_framework.authentication import TokenAuthentication
from rest_framework.authentication import SessionAuthentication
from rest_framework.authentication import BasicAuthentication
from rest_framework_jwt.authentication import JSONWebTokenAuthentication
from rest_framework import generics
from django.views.decorators.csrf import csrf_exempt

from .serializers import CommoditySerializer
from .models import Commodity
# Create your views here.


# def goods(request):
#
#     return JsonResponse({'name': 'television'})

@csrf_exempt
def login(request):
    """
    处理登录请求
    :param request: http请求对象
    :return: http响应对象
    """
    if request.method not in ('GET', 'POST'):
        return HttpResponseNotAllowed(['GET', 'POST'])

    if request.method == 'GET':
        return render(request, 'login.html')
    else:
        username = request.POST.get('username')
        password = request.POST.get('password')
        if (not username) or (not password):
            return JsonResponse({'code': 1, 'msg': 'username or password empty'})
        user = auth.authenticate(username=username, password=password)
        if user is not None:
            auth.login(request, user)
            # return JsonResponse({'code': 0, 'msg': 'login success'})
            return render(request, 'home.html')
        return JsonResponse({'code': '2', 'msg': 'username or password error'})
        # return HttpResponse(f'你提交的是POST请求:{username}密码{password}')


# 接口关联
# 先请求Login接口
# 再请求查询学生信息接口
@csrf_exempt
def logout(request):
    """
    退出登录
    :param request:
    :return:
    """
    auth.logout(request=request)
    return HttpResponseRedirect('/login')


# class GoodsView(APIView):
#     # 权限控制与认证方式控制
#
#     # restframework自带的token认证
#     # authentication_classes = [TokenAuthentication]
#     # django的session认证
#     # authentication_classes = [SessionAuthentication]
#     # jwt认证
#     authentication_classes = [JSONWebTokenAuthentication]
#     # 同时支持两种认证方式
#     # authentication_classes = [JSONWebTokenAuthentication, SessionAuthentication]
#     # 需要认证才可以访问
#     permission_classes = [IsAuthenticated]
#     # authentication_classes = []
#     # permission_classes = []
#
#     def get(self, request):
#         return Response({'name': 'television', 'price': 2000})


class CommodityListView(generics.ListCreateAPIView):
    """
    get:
        返回所有商品信息

    post:
        创建商品信息
    """

    # """
    # 列出商品信息 GET
    # 创建商品信息 POST
    # """
    # 权限控制与认证方式控制

    # rest framework自带的token认证
    # authentication_classes = [TokenAuthentication]
    # django的session认证
    # authentication_classes = [SessionAuthentication]
    # 同时支持两种认证方式
    # authentication_classes = [JSONWebTokenAuthentication, SessionAuthentication]
    # jwt认证
    authentication_classes = [JSONWebTokenAuthentication]
    # 需要认证才可以访问
    permission_classes = [IsAuthenticated]
    # authentication_classes = []
    # permission_classes = []

    queryset = Commodity.objects.all()
    serializer_class = CommoditySerializer


class CommodityView(generics.RetrieveUpdateDestroyAPIView):
    """
    get:
    返回指定编号的商品信息(Commodity Information)

    patch:
    更新指定编号的商品信息中的一个或多个字段

    put:
    更新指定编号的商品信息

    delete:
    删除指定编号的商品信息

    """

    # """
    # 对单个的商品信息进行查询(GET)
    # 对单个的商品信息进行更新(PUT 全部更新 PATCH 局部更新)
    # 对单个的商品信息进行删除(DELETE)
    # """

    authentication_classes = [JSONWebTokenAuthentication]
    permission_classes = [IsAuthenticated]
    # authentication_classes = []
    # permission_classes = []

    queryset = Commodity.objects.all()
    serializer_class = CommoditySerializer
